layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 导出设置管理
     */
    var ExportSetting = {
        tableId: "exportSettingTable"
    };

    /**
     * 初始化表格的列
     */
    ExportSetting.initColumn = function () {
        return [[
            {type: 'checkbox',fixed:'left'},
            {field: 'id', hide: true,fixed:'left', title: ''},
            {field: 'exportName',  width:200,sort: true, title: '导出名称'},
            {field: 'exportCode',  width:250,sort: true, title: '导出编号'},
            {field: 'paramClassName', width:200,sort: true, title: '参数类全路径'},
            {field: 'exportMethodName',width:130, sort: true, title: '导出方法名称'},
            {field: 'exportClassName',width:600, sort: true, title: '导出类全路径'},
            {field: 'creatorName', sort: true, title: '创建人'},
            {field: 'createTime', width:160,sort: true, title: '创建时间'},
            {align: 'center',fixed:'right', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ExportSetting.search = function () {
        var queryData = {};

        queryData['exportName'] = $('#exportName').val();
        queryData['exportCode'] = $('#exportCode').val();

        table.reload(ExportSetting.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    ExportSetting.openAddDlg = function () {
        func.open({
            title: '添加导出设置',
            height: 550,
            width: '50%',
            content: Changein.ctxPath + '/exportSetting/add',
            tableId: ExportSetting.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      ExportSetting.openEditDlg = function (data) {
          func.open({
              title: '修改导出设置',
              height: 550,
              width: '50%',
              content: Changein.ctxPath + '/exportSetting/edit?id=' + data.id,
              tableId: ExportSetting.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    ExportSetting.exportExcel = function () {
        var checkRows = table.checkStatus(ExportSetting.tableId);
        if (checkRows.data.length === 0) {
            Changein.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ExportSetting.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Changein.ctxPath + "/exportSetting/delete", function (data) {
                Changein.success("删除成功!");
                table.reload(ExportSetting.tableId);
            }, function (data) {
                Changein.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Changein.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ExportSetting.tableId,
        url: Changein.ctxPath + '/exportSetting/list',
        page: true,
        height: "full-130",
        cellMinWidth: 120,
        cols: ExportSetting.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ExportSetting.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ExportSetting.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ExportSetting.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ExportSetting.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ExportSetting.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ExportSetting.onDeleteItem(data);
        }
    });
});
